package com.gpframework.module.controller.purchase;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.module.purchase.service.IGpProcureSchedulingService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.purchase.domain.GpPurchasingMakingDetails;
import com.gpframework.module.purchase.domain.vo.GpPurchasingMakingDetailsVo;
import com.gpframework.module.purchase.domain.req.GpPurchasingMakingDetailsReq;
import com.gpframework.module.purchase.domain.req.GpPurchasingMakingDetailsAdd;
import com.gpframework.module.purchase.domain.req.GpPurchasingMakingDetailsUpdate;
import com.gpframework.module.purchase.service.IGpPurchasingMakingDetailsService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpPurchasingMakingDetailsController
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 采购做货明细Controller
 */
@Validated
@Api(tags = "采购做货明细")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/purchase/purchasingMakingDetails" )
public class GpPurchasingMakingDetailsController extends BaseController {

    private final IGpPurchasingMakingDetailsService iGpPurchasingMakingDetailsService;
    private final IGpProcureSchedulingService iGpProcureSchedulingService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-22
  * @Param gpPurchasingMakingDetails
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:list')")
    @GetMapping("/list")
    public TableDataInfo<GpPurchasingMakingDetailsVo> list(GpPurchasingMakingDetailsReq gpPurchasingMakingDetailsReq)
    {
        startPage();
        QueryWrapper<GpPurchasingMakingDetails> lqw = new QueryWrapper<GpPurchasingMakingDetails>();
        gpPurchasingMakingDetailsReq.generatorQuery(lqw,true);
        List<GpPurchasingMakingDetailsVo> list = iGpPurchasingMakingDetailsService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:add')" )
    @Log(title = "添加采购做货明细" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpPurchasingMakingDetailsAdd gpPurchasingMakingDetailsAdd) {
        GpPurchasingMakingDetails gpPurchasingMakingDetails = Convert.convert(new TypeReference<GpPurchasingMakingDetails>() {}, gpPurchasingMakingDetailsAdd);
        return toAjax(iGpPurchasingMakingDetailsService.save(gpPurchasingMakingDetails) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:edit')" )
    @Log(title = "修改采购做货明细" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpPurchasingMakingDetailsUpdate> list) {
        return toAjax(iGpPurchasingMakingDetailsService.edit(list) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:edit')" )
    @Log(title = "修改采购做货明细" , businessType = BusinessType.UPDATE)
    @PutMapping("/editOne")
    public AjaxResult edit(@Valid @RequestBody GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate) {
        return toAjax(iGpPurchasingMakingDetailsService.editOne(gpPurchasingMakingDetailsUpdate) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return AjaxResult
     */
    @ApiOperation("更新图片")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:edit')" )
    @Log(title = "更新图片" , businessType = BusinessType.UPDATE)
    @PutMapping("/updatePicture")
    public AjaxResult updatePicture(@Valid @RequestBody GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate) {
        if (gpPurchasingMakingDetailsUpdate == null || gpPurchasingMakingDetailsUpdate.getId() == null) {
            return AjaxResult.parameterError();
        }
        UpdateWrapper<GpPurchasingMakingDetails> updateLqw = new UpdateWrapper<>();
        updateLqw.set("picture", gpPurchasingMakingDetailsUpdate.getPicture());
        updateLqw.eq("id", gpPurchasingMakingDetailsUpdate.getId());
        return toAjax(iGpPurchasingMakingDetailsService.update(updateLqw) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:export')" )
    @Log(title = "导出采购做货明细" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpPurchasingMakingDetailsReq gpPurchasingMakingDetailsReq) {
        QueryWrapper<GpPurchasingMakingDetails> lqw = new QueryWrapper<GpPurchasingMakingDetails>();
        gpPurchasingMakingDetailsReq.generatorQuery(lqw,true);
        List<GpPurchasingMakingDetailsVo> list = iGpPurchasingMakingDetailsService.listVo(lqw);
        ExcelUtil<GpPurchasingMakingDetailsVo> util = new ExcelUtil<GpPurchasingMakingDetailsVo>(GpPurchasingMakingDetailsVo. class);
        return util.exportExcel(list, "purchasingMakingDetails" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpPurchasingMakingDetailsVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpPurchasingMakingDetailsService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchasingMakingDetails
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:remove')" )
    @Log(title = "删除采购做货明细" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpPurchasingMakingDetailsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成生产排单表
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("生成采购排单表")
    @PreAuthorize("@ss.hasPermi('purchase:procureScheduling:add')" )
    @Log(title = "生成采购排单表" , businessType = BusinessType.INSERT)
    @PostMapping("/createProcureScheduling")
    public AjaxResult createProcureScheduling(@RequestBody GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate) {
        if (gpPurchasingMakingDetailsUpdate == null || CollectionUtils.isEmpty(gpPurchasingMakingDetailsUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcureSchedulingService.createProcureScheduling(gpPurchasingMakingDetailsUpdate.getIds(), gpPurchasingMakingDetailsUpdate.getCreateBy()) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('purchase:purchasingMakingDetails:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate) {
        if (gpPurchasingMakingDetailsUpdate == null || gpPurchasingMakingDetailsUpdate.getId() == null || gpPurchasingMakingDetailsUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpPurchasingMakingDetailsUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpPurchasingMakingDetailsService.copy(gpPurchasingMakingDetailsUpdate.getId(), gpPurchasingMakingDetailsUpdate.getCopyNum()) ? 1 : 0);
    }
}
